/**
 * Created by jackliu on 15/12/6.
 */

import SJGPage = require('./../kernel/cont/SJGPage');
import ObjManager = require('./../kernel/cont/ObjManager');
import TextObj = require('./../kernel/obj/TextObj');
import ChartObj = require('./../kernel/obj/ChartObj');
import GroupObj = require('./../kernel/obj/GroupObj');
import SJGReplaceGroup = require('./../kernel/replace/SJGReplaceGroup');
import SJGReplaceItemImage = require('./../kernel/replace/SJGReplaceItemImage');
import SJGReplaceItemText = require('./../kernel/replace/SJGReplaceItemText');
import SJGReplaceItemChart = require('./../kernel/replace/SJGReplaceItemChart');
import SJGTypeSetItemBase = require('./SJGTypeSetItemBase');
import SJGTypeSetBoxUtil2 = require('./SJGTypeSetBoxUtil2');
import ObjBase = require('./../kernel/obj/ObjBase');
import CommonUtil = require('./../../util/CommonUtil');
import ObjPositionAlign = require('./../kernel/base/ObjPositionAlign');
import GCJ_CLASS_TYPE= require('./../common/GCJ_CLASS_TYPE');
import ParaContour = require('./../common/graph/ParaContour');
var async  = require( 'async'); // 同步控制
export = SJGTypeSetChartGroup;

class SJGTypeSetChartGroup extends  SJGTypeSetItemBase{

    // 根据坐标和宽高排版对象，需要被重载
    // 参数：p页面
    // 参数：group标题组
    // 参数：i可替换对象的下标
    // 参数：pos对象的宽高
    public typesetObject(p:SJGPage,group: SJGReplaceGroup,i:number, pos:ObjPositionAlign){
        console.log("=->runhere0")

        var objMgr:ObjManager = p.getGlobalData().objManager();
        var groupObj:GroupObj = <GroupObj>objMgr.getObject(group.id);// 原版面中的对象
        var replaceItemChart:SJGReplaceItemChart = <SJGReplaceItemChart>group.value[i];
        var G:GroupObj = null;

        if(i<groupObj.children.length){
            // 修改旧对象GroupObj G以及其文字子对象ChartObj C
            G =<GroupObj> groupObj.children[i];
        }else{
            // 创建新对象,深拷贝最后一个子GroupObj
            // 得到GroupObj G以及其文字子对象ChartObj C
            G =<GroupObj> CommonUtil.deepCopy(groupObj.children[groupObj.children.length-1],p.getGlobalData().ver,p.getGlobalData());
        }
        var arr:Array<number> = G.getChildOfType(GCJ_CLASS_TYPE.CLASS_TYPE_ChartObj);
        var T:ChartObj =<ChartObj> objMgr.getObject(arr[0]);
        if(!T)  return;

        // G的位置尺寸设置为pos
        //G.pos.copy(pos);

        // T的位置尺寸设置为pos
        //T.pos.copy(pos)

        // 从replaceItemChart拷贝信息到T中
        T.copyInfo(replaceItemChart,p.getGlobalData().ver);

        // 描绘
        T.refresh();


        // 对于圆形扩展，不进行缩放，移动位置即可
        if(group.advance.extend_method == SJGReplaceGroup.CIRCLE){
            G.align = pos;
            var ap = pos.toPos();
            G.pos.copy(ap);

            return;
        }

        // 调整编组G和内部子对象的大小
        //var arr:Array<number> = G.getChildOfType(GCJ_CLASS_TYPE.CLASS_TYPE_ChartObj,true);
        //for(var k in arr){
        //    var other:ObjBase =  objMgr.getObject(arr[k]);
        //    other.constrain.scale = 0; // 将可变对象之外的所有对象约束设置为固定
        //}

        var ret:Array<number> = SJGTypeSetBoxUtil2.calcRatioXY(groupObj.children.length, group.value.length,
            group.advance.extend_method,group.advance.limit);
        G.expand(1, ret[0],  1,      false); // 向右扩展
        G.expand(2, 1,       ret[1], false);// 向下扩展

    }
}
